package com.fc.v2.controller.gen;

import com.fc.v2.common.base.BaseController;
import com.fc.v2.common.domain.AjaxResult;
import com.fc.v2.common.domain.ResultTable;
import com.fc.v2.model.auto.*;
import com.fc.v2.model.custom.Tablepar;
import com.fc.v2.service.CategoryService;
import com.fc.v2.service.PoetryCategoryService;
import com.fc.v2.service.PoetryService;
import com.fc.v2.util.BeanUtils;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 诗分类Controller
 * @ClassName: PoetryCategoryController
 * @author fuce
 * @date 2021-03-04 14:37:32
 */
@Api(value = "诗分类")
@Controller
@RequestMapping("/PoetryCategoryController")
public class PoetryCategoryController extends BaseController{
	
	private String prefix = "gen/poetryCategory";
	
	@Autowired
	private PoetryCategoryService poetryCategoryService;
	@Autowired
	private PoetryService poetryService;
	@Autowired
	private CategoryService categoryService;
	/**
	 * 诗分类页面展示
	 * @param model
	 * @return String
	 * @author fuce
	 */
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/view")
	@RequiresPermissions("gen:poetryCategory:view")
    public String view(ModelMap model)
    {
        return prefix + "/list";
    }
	
	/**
	 * list集合
	 * @param tablepar
	 * @param searchText
	 * @return
	 */
	//@Log(title = "诗分类", action = "111")
	@ApiOperation(value = "分页跳转", notes = "分页跳转")
	@GetMapping("/list")
	@RequiresPermissions("gen:poetryCategory:list")
	@ResponseBody
	public ResultTable list(Tablepar tablepar,PoetryCategory poetryCategory){
		PageInfo<PoetryCategory> page=poetryCategoryService.list(tablepar,poetryCategory) ;
		List<Wapper> res = getWapper(page);
		return pageTable(res,page.getTotal());
	}

	private List<Wapper> getWapper(PageInfo<PoetryCategory> page){
		List<PoetryCategory> list = page.getList();
		ArrayList<Wapper> res = new ArrayList<>();
		for(PoetryCategory p:list){
			Wapper wapper = new Wapper();
			BeanUtils.copyBeanProp(wapper,p);
			Poetry poetry = poetryService.selectByPrimaryKey(String.valueOf(p.getPid()));
			wapper.setTitle(poetry.getTitle());
			wapper.setAuthor(poetry.getAuthor());

			Category category = categoryService.selectByPrimaryKey(String.valueOf(p.getCid()));
			wapper.setCategoryName(category.getName());
			res.add(wapper);
		}
		return res;
	}
	
	/**
     * 新增跳转
     */
	@ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
		return prefix + "/add";
    }
	
    /**
     * 新增保存
     * @param 
     * @return
     */
	//@Log(title = "诗分类新增", action = "111")
	@ApiOperation(value = "新增", notes = "新增")
	@PostMapping("/add")
	@RequiresPermissions("gen:poetryCategory:add")
	@ResponseBody
	public AjaxResult add(PoetryCategory poetryCategory){
		int b=poetryCategoryService.insertSelective(poetryCategory);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	/**
	 * 诗分类删除
	 * @param ids
	 * @return
	 */
	//@Log(title = "诗分类删除", action = "111")
	@ApiOperation(value = "删除", notes = "删除")
	@DeleteMapping("/remove")
	@RequiresPermissions("gen:poetryCategory:remove")
	@ResponseBody
	public AjaxResult remove(String ids){
		int b=poetryCategoryService.deleteByPrimaryKey(ids);
		if(b>0){
			return success();
		}else{
			return error();
		}
	}
	
	
	/**
	 * 修改跳转
	 * @param id
	 * @param mmap
	 * @return
	 */
	@ApiOperation(value = "修改跳转", notes = "修改跳转")
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map)
    {
        map.put("PoetryCategory", poetryCategoryService.selectByPrimaryKey(id));

        return prefix + "/edit";
    }
	
	/**
     * 修改保存
     */
    //@Log(title = "诗分类修改", action = "111")
	@ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("gen:poetryCategory:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(PoetryCategory poetryCategory)
    {
        return toAjax(poetryCategoryService.updateByPrimaryKeySelective(poetryCategory));
    }


    class Wapper extends PoetryCategory{
		String title;
		String author;
		String categoryName;

		public String getTitle() {
			return title;
		}

		public void setTitle(String title) {
			this.title = title;
		}

		public String getAuthor() {
			return author;
		}

		public void setAuthor(String author) {
			this.author = author;
		}

		public String getCategoryName() {
			return categoryName;
		}

		public void setCategoryName(String categoryName) {
			this.categoryName = categoryName;
		}
	}
    
    

	
}
